<?php
require_once('includes/config.php');
require_once('includes/functions/func.global.php');
require_once('includes/functions/func.cron.php');
require_once('includes/lang/lang_'.$config['lang'].'.php');

// Connect to database
db_connect($config);

// Get Settings
$settings = get_settings('cron.php',$config);

ignore_user_abort(1);
@set_time_limit(0);

if($config['cron_type'] == '1')
{
	$filedata = base64_decode('R0lGODlhAQABAIAAAMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
	$filesize = strlen($filedata);
	
	header('Content-type: image/gif');
	header('Content-Length: ' . $filesize);
	header('Connection: Close');
	
	echo $filedata;
		
	if((time()-300) > $settings['cron_time'])
	{
		flush();
	}
	else
	{
		exit;
	}
}
else
{
	echo 'run successfully';
}

$start_time = time();

update_setting($config,'cron_time',time());

// Retrieve stories from the database
$query = "SELECT story_id,story_time,story_digs,story_title,story_buries,story_prom FROM ".$config['db']['pre']."stories WHERE story_time>'".(time()-86400)."' LIMIT 600";
$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
while ($info = @mysql_fetch_array($query_result))
{
	$promote = false;
	$ago = round(((time()-$info['story_time'])/60));
	$digs = $info['story_digs'];
	$buries = $info['story_buries'];
	$days = floor(($ago/60)/24);
	
	$rate = ($ago/$digs);
	
	if($info['story_time'] < (time()-43200))
	{
		$rate = 99999;
	}
	elseif($buries > 5)
	{
		$rate = 99999;
	}
	else
	{
		if(!$info['story_prom'])
		{
			if($digs >= $config['rating_digs'])
			{
				if($config['rating_days'] > $days)
				{
					$promote = true;
				}
			}
		}
	}

	if($promote)
	{
		mysql_query("UPDATE `".$config['db']['pre']."stories` SET `story_rating` = '".validate_input($rate)."',`story_prom` = '1' ,`story_prom_date` = '".time()."' WHERE `story_id` =".$info['story_id']." LIMIT 1;");
	}
	else
	{
		mysql_query("UPDATE `".$config['db']['pre']."stories` SET `story_rating` = '".validate_input($rate)."' WHERE `story_id` =".$info['story_id']." LIMIT 1;");
	}
}

$feed_links = 0;
$feeds_checked = 0;

$query = "SELECT feed_id,feed_url,feed_cat,feed_user,feed_period,feed_lastch FROM ".$config['db']['pre']."feeds LIMIT 20";
$query_result = @mysql_query ($query) OR error(mysql_error(), __LINE__, __FILE__, 0, '', '');
while ($info = @mysql_fetch_array($query_result))
{
	$username = '';

	if((time()-$info['feed_period']) > $info['feed_lastch'])
	{
		$xdata = readXMLfile($info['feed_url']);
		
		mysql_query("UPDATE `".$config['db']['pre']."feeds` SET `feed_lastch` = '".time()."' WHERE `feed_id` = ".$info['feed_id']." LIMIT 1 ;");
			
		foreach ($xdata as $key => $value)
		{
			$check_feed = mysql_num_rows(mysql_query("SELECT 1 FROM `".$config['db']['pre']."feeds_log` WHERE feed_hash='".validate_input(md5($value->link))."' LIMIT 1"));			
			
			if($check_feed == 0)
			{			
				if($username == '')
				{
					$user_lookup = mysql_fetch_row(mysql_query("SELECT username FROM ".$config['db']['pre']."users WHERE user_id='".validate_input($info['feed_user'])."' LIMIT 1"));
					$username = $user_lookup[0];
				}
						
				mysql_query("INSERT INTO `".$config['db']['pre']."stories` ( `story_id` , `user_id` , `user_name` , `story_url` , `story_title` , `story_desc` , `story_cat` , `story_rating` , `story_digs` , `story_time` , `story_thumb` ) VALUES ('', '".$info['feed_user']."', '".$username."', '".validate_input($value->link)."', '".validate_input($value->title)."', '".validate_input(substr(strip_tags($value->description),0,400))."', '".validate_input($info['feed_cat'])."', '9999', '1', '".time()."','');");
				mysql_query("INSERT INTO `".$config['db']['pre']."feeds_log` ( `feed_id` , `feed_hash` , `feed_time` ) VALUES ('".$info['feed_id']."', '".validate_input(md5($value->link))."', '".time()."');");
			
				$feed_links++;
			}
		}
		
		$feeds_checked++;
	}
}

$end_time = (time()-$start_time);

$cron_details = "Feeds Checked: ".$feeds_checked."<br>";
$cron_details.= "Feed Links Added: ".$feed_links."<br>";
$cron_details.= "<br>";
$cron_details.= "Cron Took: ".$end_time." seconds";

log_adm_action($config,'Cron Run',$cron_details);
?>